package com.zzty.txj.controller;

import com.zzty.txj.entity.Daily;
import com.zzty.txj.entity.DoDaily;
import com.zzty.txj.entity.Record;
import com.zzty.txj.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/record")
public class RecordController {
    @Autowired
    RecordService recordServiceImpl;
    @ResponseBody
    @RequestMapping(value="/getDoDaily")
    public List<Record> getRecord(DoDaily doDaily, HttpServletRequest request){
        Date date=new Date();
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String date1=String.valueOf(df1.format(date));
        doDaily.setDate(date1);
         String openId =(String)request.getSession().getAttribute("openId");

         Integer id = recordServiceImpl.getUserId(openId);

         doDaily.setUserId(id);
        System.out.println(doDaily.toString());
        List<DoDaily> doDailies = recordServiceImpl.getDoDaily(doDaily);

        List<Record>records =new ArrayList<>();
        for(int i=0;i<doDailies.size();i++){
            Record record=new Record();
            record.setName(recordServiceImpl.getDailyName(doDailies.get(i).getDailyId()));
            record.setFinalResult(String.valueOf(doDailies.get(i).getFinalResult()));

            if(null==doDailies.get(i).getResult()||-1==doDailies.get(i).getResult()){
                record.setResult("未完成");
            }
            else if(1==doDailies.get(i).getResult()){
                record.setResult("完成");
            }
            else{
                record.setResult("请假");
            }
            records.add(record);
        }

        return  records;
    }
}
